﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Linq;
using System.Security.Claims;
using WZN_shop.Models.Database;
using WZN_shop.service;

namespace WZN_shop.Controllers
{
    [Route("[controller]/[action]")]
    [ApiController]
    public class UsersController : ControllerBase
    {
        private readonly IJWTService _jwtService;
        private readonly WZN_ShopContext _db;
        public UsersController(IJWTService jwtService, WZN_ShopContext db)
        {
            _jwtService = jwtService;
            _db = db;
        }
    /// <summary>
    /// 用户注册
    /// </summary>
    /// <param name="phone"></param>
    /// <param name="password"></param>
    /// <param name="password2"></param>
    /// <param name="name"></param>
    /// <param name="hand"></param>
    /// <param name="email"></param>
    /// <returns></returns>
        [HttpPost]
        public string Registered(string phone, string password, string password2, string name, string hand, string email)
        {
            if (password == password2)
            {
                User user = new Models.Database.User();
                user.Phone = phone;
                user.Password = password;
                user.Name = name;
                user.Hand = hand;
                user.Email = email;
                User users = _db.Users.FirstOrDefault(x => x.Phone == phone);
                if (users != null)
                {
                    return "该手机已注册";
                }
                else
                {
                    _db.Users.Add(user);
                    if (_db.SaveChanges() > 0)
                    {
                        return "注册成功";
                    }
                    else
                    {
                        return "注册失败";
                    }
                }
            }
            else
            {
                return "密码不一致";
            }
        }

        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="phone"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        [HttpPost]
        public string Login(string phone,string password)
        {
            User user=_db.Users.FirstOrDefault(x => x.Phone == phone);
            if (user == null)
            {
                return "用户不存在，请先注册";
            }
            //return user.Password;
            if(user.Password == password)
            {
                return _jwtService.CreateToken(user.UserId.ToString(),user.Phone,user.Password,user.Name,user.Hand,user.Email,user.Createtime.ToString());
            }
            else
            {
                return "密码错误";
            }
        }

        /// <summary>
        /// 个人中心
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Authorize]
        public string userinfo()
        {
            //return Response.HttpContext.User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier).Value;
            var userId =int.Parse(Response.HttpContext.User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier).Value);
            var finish = _db.Porders.Where(x => x.UserId == userId && x.Ostatic == "已完成").Count();
            var Dispatch = _db.Porders.Where(x => x.UserId == userId && x.Ostatic == "派送中").Count();
            var pay = _db.Porders.Where(x => x.UserId == userId && x.Ostatic == "已付款").Count();
            var refund=_db.Porders.Where(x=>x.UserId==userId&&x.Ostatic=="已退款").Count();
            var hand = Response.HttpContext.User.Claims.FirstOrDefault(x => x.Type == "hand").Value;
            var name = "";
            if((Response.HttpContext.User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.Name).Value) != null)
            {
                name = Response.HttpContext.User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.Name).Value;
            }
            else
            {
                name = Response.HttpContext.User.Claims.FirstOrDefault(x => x.Type == "phone").Value;
            }
            var result = "已完成订单：" + finish + "\n派送中订单：" + Dispatch + "\n已付款" + pay + "\n已退款" + refund + "\n头像" + hand + "\n昵称：" + name;
            return result;
        }


        /// <summary>
        /// 修改用户信息
        /// </summary>
        /// <param name="hand"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        [HttpGet]
        [Authorize]
        public string ChangeUserInfo(string hand,string name)
        {
            var userId = int.Parse(Response.HttpContext.User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier).Value);
            var user=_db.Users.FirstOrDefault(x=>x.UserId == userId);
            if (hand == ""&&name!="")
            {
                user.Name= name;
                _db.SaveChanges();
                return "修改成功";
            }else if (hand != "" && name == "")
            {
                user.Hand= hand;
                _db.SaveChanges();
                return "修改成功";
            }else
            {
                return "请填入修改信息";
            }
        }
    }
}
